﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class 废料入库_In : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            int BillID = 0;
            string list;

            if (!this.IsPostBack)
            {

                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                list = Request.QueryString["list"];

                if (BillID > 0)
                {
                    this.Label_ID.Text = BillID.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = this.Label_ID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);

                }
                else if (list != null)
                {
                    this.Date1.Text = DateTime.Today.ToLongDateString();
                    if (list.EndsWith(","))
                    {
                        list = list.Substring(0, list.Length - 1);
                    }
                    string[] tmp = list.Split(",".ToCharArray());
                    int i, t;
                    for (i = 0; i < tmp.Length; i++)
                    {
                        if (!int.TryParse(tmp[i], out t))
                        {
                            this.Notice = "到货记录列表有误";
                            this.Button_Save.Enabled = false;
                            return;
                        }
                    }
                    this.SqlDataSource1.SelectCommand =
@"select top 1
@废料ID=t1.废料ID
,@单位ID=t1.单位ID
,@废料名称=t2.废料名称
,@规格=t2.规格
,@单位=t2.单位
,@单位名称=t3.名称
from 废料到发记录 t1
left outer join 废料 t2 on t2.ID=t1.废料ID
left outer join 生产单位 t3 on t3.ID=t1.单位ID
where t1.发货=0  and t1.ID in(" + list + ");";
                    this.SqlDataSource1.SelectCommand +=
@"select @记录数=count(*)
,@数量0=0
,@数量=cast(sum(数量) as float)
,@单价=0
from 废料到发记录
where 废料ID=@废料ID
and 单位ID=@单位ID 
and 发货=0 and ID in(" + list + ");";
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                    if (int.Parse(this.Label_Record.Text) != tmp.Length)
                    {
                        this.Button_Save.Enabled = false;
                        this.Notice = "";
                    }
                    else
                    {
                        this.Label_List.Text = list;
                    }
                }
                else
                {
                    this.Button_Save.Enabled = false;
                }
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (this.SqlDataSource1.SelectParameters["ID"].DefaultValue != "0")
            {
                if (Convert.IsDBNull(e.Command.Parameters[1].Value))
                {
                    this.Notice = "材料入库单读取失败";
                    this.Button_Save.Enabled = false;
                    return;
                }
            }

            if (!Convert.IsDBNull(e.Command.Parameters[1].Value))
            {
                this.Label_CLID.Text = ((int)e.Command.Parameters[1].Value).ToString();
                this.Label_BianMa.Text = ((int)e.Command.Parameters[1].Value).ToString("D10");
            }
            if (!Convert.IsDBNull(e.Command.Parameters[2].Value))
            {
                this.Label_DWID.Text = ((int)e.Command.Parameters[2].Value).ToString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
            {
                //this.Label_ShuLiang0.Text = ((double)e.Command.Parameters[3].Value).ToString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[4].Value))
            {
                this.Label_ShuLiang.Text = ((double)e.Command.Parameters[4].Value).ToString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[5].Value))
            {
                this.Label_DanJia.Text = ((double)e.Command.Parameters[5].Value).ToString();
                this.DanJia.Text = this.Label_DanJia.Text;
                if (this.Label_ShuLiang.Text != "")
                {
                    this.Label_JinE.Text = (((double)e.Command.Parameters[4].Value) * ((double)e.Command.Parameters[5].Value)).ToString();
                }
            }

            if (!Convert.IsDBNull(e.Command.Parameters[6].Value))
            {
                this.Date1.Text = ((DateTime)e.Command.Parameters[6].Value).ToLongDateString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[7].Value))
            {
                this.BeiZhu.Text = (string)e.Command.Parameters[7].Value;
            }


            if (!Convert.IsDBNull(e.Command.Parameters[8].Value))
            {
                this.Label_CaiLiao.Text = (string)e.Command.Parameters[8].Value;
            }
            if (!Convert.IsDBNull(e.Command.Parameters[9].Value))
            {
                this.Label_GuiGe.Text = (string)e.Command.Parameters[9].Value;
            }
            if (!Convert.IsDBNull(e.Command.Parameters[10].Value))
            {
                this.Label_DanWei.Text = (string)e.Command.Parameters[10].Value;
            }

            if (!Convert.IsDBNull(e.Command.Parameters[11].Value))
            {
                this.Label_GongHuo.Text = (string)e.Command.Parameters[11].Value;
            }
            if (!Convert.IsDBNull(e.Command.Parameters[12].Value))
            {
                this.Label_Record.Text = ((int)e.Command.Parameters[12].Value).ToString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[13].Value))
            {
                this.DanHao.Text = ((int)e.Command.Parameters[13].Value).ToString();
            }
        }

        protected void SqlDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e)
        {           
           
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            this.Label_ID.Text = ((int)e.Command.Parameters[0].Value).ToString();
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            DateTime d;
            if (!DateTime.TryParse(this.Date1.Text, out d))
            {
                this.Notice = "“日期”录入有误。";
                return;
            }
            int dh = 0;
            if (!int.TryParse(this.DanHao.Text, out dh) || dh <= 0)
            {
                this.Notice = "“单号”录入有误，请录入一个大于零的整数。";
                return;
            }
            decimal dj = 0;
            if (!decimal.TryParse(this.DanJia.Text, out dj) || dj <= 0)
            {
                this.Notice = "“单价”录入有误，请录入一个大于零的数字。";
                return;
            }
            if (this.Label_ID.Text == "0")
            {
                string op = (string)this.Session["OperatorName"];
                if (op == null)
                {
                    op = "";
                }
                this.SqlDataSource1.InsertParameters["保管员"].DefaultValue = op;
                this.SqlDataSource1.InsertCommand =
@"begin transaction;
declare @结存数量 numeric(18,4);
declare @结存金额 numeric(27,10);
set @结存数量=0;
set @结存金额=0;
select top 1 @结存数量=isnull(结存数量,0),@结存金额=isnull(结存金额,0) from 废料入出库 with(tablockx) where 废料ID=@废料ID order by ID desc;
set @结存数量=@结存数量+@数量;
set @结存金额=@结存金额+@数量*@单价;
insert into 废料入出库(单据号,废料ID,单位ID,日期,数量0,数量,单价,结存数量,结存金额,备注,保管员)values
(@单据号,@废料ID,@单位ID,@日期,0,@数量,@单价,@结存数量,@结存金额,@备注,@保管员);
set @ID=@@IDENTITY;
update 废料到发记录 set 单据ID=@ID where ID IN(" + this.Label_List.Text + ");";
                this.SqlDataSource1.InsertCommand +=
@"
if @记录数=@@ROWCOUNT
begin
commit;
end
else
begin
rollback;
set @ID=0;
end";
                this.SqlDataSource1.Insert();
                if (this.Label_ID.Text == "0")
                {
                    this.Notice = "保存失败";
                }
                else
                {
                    this.Notice = "保存成功";
                }
            }
            else
            {
                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Notice = "保存成功";
                }
                else
                {
                    this.Notice = "保存失败";
                }

            }
        }
    }
}
